1 #include "BookDatabase.h"
2
3 BookDatabase::BookDatabase()
4 {
5 used = 0;
6 capacity = 5;
7 bookPtr = new Book[capacity];
8 }
9
10 void BookDatabase::addBook(const Book& newBook)
11 {
12 if (used >= capacity)
13 {
14 cout << "Database is full, Doubling Size. . ." << endl;
15 make_bigger();
16 }
17 bookPtr[used] = newBook;
18 used++;
19 }
20 void BookDatabase::printall() const
21 {
22 for (int i = 0; i < used; i++)
23 bookPtr[i].printBook();
24 }
25
26 void BookDatabase::editBookAt(int index)
27 {
28
29 cout << "What would you want to edit?" << endl;
30 cout << " 1. Title" << endl;
31 cout << " 2. BookID" << endl;
32 cout << " 3. Author" << endl;
33 cout << " 4. Publisher" << endl;
34 cout << " 5. Publication Date" << endl;
35 cout << " 6. Edition" << endl;
36 cout << " 7. Cost" << endl;
37
38 int choice, intEdit = 0;
39 string stringEdit;
40
41 cout << "\nEnter your choice: ";
42 cin >> choice;
43 cout << endl;
44
45 while (choice != 8)
46 {
47 switch (choice)
48 {
49 case 1:
50 cout << "Enter new Title" << endl;
51 cin.ignore(1, ' ');
52 getline(cin, stringEdit);
53 bookPtr[index].setTitle(stringEdit);
54
55 break;
56
57 case 2:
58 cout << "Enter new bookID" << endl;
59 cin.ignore(1, ' ');
60 getline(cin, stringEdit);
61 bookPtr[index].setBookID(stringEdit);
62
63 break;
64
65 case 3:
66 cout << "Enter new author" << endl;
67 cin.ignore(1, ' ');
68 getline(cin, stringEdit);
69 bookPtr[index].setAuthor(stringEdit);
70
71 break;
72 case 4:
73 cout << "Enter new Publisher" << endl;
74 cin.ignore(1, ' ');
75 getline(cin, stringEdit);
76 bookPtr[index].setPublisher(stringEdit);
77
78 break;
79
80 case 5:
81 cout << "Enter new publication date" << endl;
82 cin >> intEdit;
83 bookPtr[index].setPublication(intEdit);
84
85 break;
86
87 case 6:
88 cout << "Enter new edition number" << endl;
89 cin >> intEdit;
90 bookPtr[index].setEdition(intEdit);
91
92 break;
93
94 case 7:
95 cout << "Enter new cost" << endl;
96 cin >> intEdit;
97 bookPtr[index].setCost(intEdit);
98
99 break;
100
101 default:
102 cout << "Invalid Selection" << endl;
103
104 break;
105 }
106
107 cout << endl;
108 system("Pause");
109 cout << endl;
110 cout << "What would you want to edit?" << endl;
111 cout << " 1. Title" << endl;
112 cout << " 2. BookID" << endl;
113 cout << " 3. Author" << endl;
114 cout << " 4. Publisher" << endl;
115 cout << " 5. Publication Date" << endl;
116 cout << " 6. Edition" << endl;
117 cout << " 7. Cost" << endl;
118 cout << "\nEnter your choice: ";
119 cin >> choice;
120 cout << endl;
121 }
122
123 }
124
125 Book BookDatabase::getBook(int index) const
126 {
127 return bookPtr[index];
128 }
129
130 void BookDatabase::printBookTitle() const
131 {
132 for (int i = 0; i < used; i++)
133 cout << i + 1 <<". " << bookPtr[i].getTitle() << endl;
134 }
135
136 void BookDatabase::saveToFile(ofstream& out)
137 {
138 if (used == 0)
139 cerr << "There is nothing to save" << endl;
140 else
141 {
142 for (int i = 0; i < used; i++)
143 bookPtr[i].printBookToFile(out);
144
145 out << endl;
146 out << "END";
147 }
148
149 }
150
151 void BookDatabase::make_bigger()
152 {
153 Book *temp;
154 temp = new Book[capacity * 2];
155 for (int i = 0; i < used; i++)
156 temp[i] = bookPtr[i];
157 delete[] bookPtr;
158 bookPtr = temp;
159 capacity *= 2;
160 }
161
162 BookDatabase::~BookDatabase()
163 {
164
165 }